﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;


namespace IHISCETeim.Web.MachineSys.XZModel
{
    public partial class XZJobTrainDetail : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除, };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "XZJobTrainList";
            }
        }

        protected int JobId
        {
            get
            {
                return GetRequestValue<int>("JobId", 0);
            }
        }

        private XZJobListInfo _XZJobListObj = null;
        protected XZJobListInfo XZJobListObj
        {
            get
            {
                if (_XZJobListObj == null)
                    _XZJobListObj = XZJobTrainController.GetJobByJobId(JobId);
                return _XZJobListObj;
            }
        }

        private List<XZJobTrainInfo> _CurrJobTrainDetail = null;
        /// <summary>
        /// 当前岗位已有的培训内容
        /// </summary>
        protected List<XZJobTrainInfo> CurrJobTrainDetail
        {
            get {
                if(_CurrJobTrainDetail==null)
                    _CurrJobTrainDetail=XZJobTrainController.GetAllByJobId(JobId);
                return _CurrJobTrainDetail;
            }
        }
        private List<XZTrainListInfo> _AllJobTrainDetail = null;
        /// <summary>
        /// 所有部门的培训内容
        /// </summary>
        protected List<XZTrainListInfo> AllJobTrainDetail
        {
            get
            {
                if (_AllJobTrainDetail == null)
                    _AllJobTrainDetail = XZJobTrainController.GetAllXZTrainList("");
                return _AllJobTrainDetail;                
            }
        }
        private DataTable _AllTrainDept = null;
        /// <summary>
        /// 获取所有有培训内容的部门名称
        /// </summary>
        protected DataTable AllTrainDept
        {
            get
            {
                if (_AllTrainDept == null)
                    _AllTrainDept = XZJobTrainController.XZTrainList_GetAllDept();
                return _AllTrainDept;      
            }
        }


        protected string GetChecked(object _TrainId)
        {
            int TrainId = Convert.ToInt32(_TrainId);
            List<XZJobTrainInfo> templist=  CurrJobTrainDetail.FindAll(e => e.JobId == JobId && e.TrainId == TrainId);
            return templist.Count > 0?"checked":"";
        }
        protected string GetClassHour(object _TrainId, object _ClassHour)
        {
            int TrainId = Convert.ToInt32(_TrainId);
            try
            {
                XZJobTrainInfo templist = CurrJobTrainDetail.First<XZJobTrainInfo>(e => e.JobId == JobId && e.TrainId == TrainId);
                if (templist != null)
                    return templist.ClassHour.ToString();
                return "0";
            }
            catch
            {
                return "0";
            }
           
        }
        protected string GetUnit(object _TrainId, object _Unit)
        {
            int TrainId = Convert.ToInt32(_TrainId);
            string selval = "";
            try
            {
                XZJobTrainInfo templist = CurrJobTrainDetail.First<XZJobTrainInfo>(e => e.JobId == JobId && e.TrainId == TrainId);
                if (templist != null)
                    selval=templist.Unit.ToString();
            }
            catch
            {
                selval= "";
            }

            selval = selval.Trim().ToUpper();
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<select name='Unit'>");

            string[] arr = new string[] { "", "小时", "天", "月" };
            foreach (string itm in arr)
            {
                sb.AppendFormat("<option value='{0}' {1}>{0}</option>", itm, itm.Trim().ToUpper() == selval ? "selected" : "");
            }
            sb.AppendFormat("</select>");

            return sb.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "update")
            {
                addmenu();
            }
            else
            {
                this.rpMenu.DataSource = AllTrainDept;
                this.rpMenu.DataBind();
            }
        }

        protected string GetSubTrainHtml(object _TrainDept)
        {
            /*
                <tr>
                    <td><input type="checkbox" name="checkselected"  value="<%# Eval("TrainId")%>" <%# GetChecked(Eval("TrainId")) %>  /></td>
                    <td><%# Eval("TrainDept")%></td>
                    <td><%# Eval("TrainContent")%></td>
                    <td><input type="text"  size="3" name="ClassHour" value="<%# GetClassHour(Eval("TrainId"),Eval("ClassHour")) %>"  /></td>
                    <td><input type="text" size="3" name="Unit" value="<%# GetUnit(Eval("TrainId"),Eval("Unit")) %>"  /></td>
                </tr>
             */
            string TrainDept = _TrainDept.ToString();

            List<XZTrainListInfo> list=AllJobTrainDetail.Where<XZTrainListInfo>(e=>e.TrainDept==TrainDept).ToList();

            StringBuilder sb=new StringBuilder();
            foreach(XZTrainListInfo itm in list)
            {
                sb.AppendFormat(@"
                 <tr>
                    <td align='right' style=' text-align:right;'><input type='checkbox' name='checkselected' SubTrainDept='{5}'  value='{0}' {1}/></td>
                   
                    <td>{2}</td>
                    <td><input type='text'  size='3' name='ClassHour' value='{3}'  /></td>
                    <td>{4}</td>
                </tr>",
                itm.TrainId, GetChecked(itm.TrainId), itm.TrainContent, GetClassHour(itm.TrainId, itm.ClassHour), GetUnit(itm.TrainId, itm.Unit), _TrainDept
                );
            }

            return sb.ToString();
        }

        protected void addmenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] checkselecteds = GetRequestValue<string>("checkselected", "").Split(caSplit);
            string[] ClassHours = GetRequestValue<string>("ClassHour", "").Split(caSplit);
            string[] Units = GetRequestValue<string>("Unit", "").Split(caSplit);

            List<XZJobTrainInfo> list = new List<XZJobTrainInfo>();
            for (int index = 0; index < checkselecteds.Count(); index++)
            {
                XZJobTrainInfo itm = new XZJobTrainInfo();
                itm.JobId = JobId;
                itm.TrainId = Convert.ToInt32(checkselecteds[index]);
                itm.Unit = Units[index];
                itm.ClassHour = ClassHours[index];

                list.Add(itm);
            }

            bool r = XZJobTrainController.AddXZJobTrain(JobId,list);


          //  this.AddLog(Permission.新增, null, obj);

            if (r)
                SuccessJson("成功保存");
            else
                ErrorJson("保存失败");

        }
    }
}